<script type='text/javascript'>
chrome.contextMenus.create({"title":"Open tabs",
                            "onclick":launcher_tools,"contexts":['selection']});
localStorage['urlStorage'] = 0;
/**
 * lsId - localStorage Id in which the created tabId will be stored
 * acturl - The URL to be launched
 * selected_option - Pass true to switch focus
 * isrequire - Pass True if you want the created tab to get updated
 */
function launchthat(lsId, acturl, selected_option,irequire) {
  if(localStorage[lsId] == 0) {
    chrome.tabs.create({"url":acturl,"selected":selected_option},function(tab) {
      localStorage[lsId] = tab.id;
      //Adding removal Listener
      chrome.tabs.onRemoved.addListener(function(tabId) { 
        if(tabId == localStorage[lsId]) {
          localStorage[lsId] = 0;
        }
      });
      //Adding update Listener
      chrome.tabs.onUpdated.addListener(function(tabId,info,tab) {
        if(tabId  ==  localStorage[lsId]) {
          localStorage[lsId] = tab.id;
        }
      });
    });
  } else {
    if(irequire == true) {      
      chrome.tabs.update(parseInt(localStorage[lsId]),
        {"url":acturl,"selected":selected_option},function(tab) {
          //Adding removal Listener to the updated tab.
          chrome.tabs.onRemoved.addListener(function(tabId) { 
            if(tabId  ==  localStorage[lsId]) {
              localStorage[lsId] = 0; 	
            }
          });
          //Adding update Listener to the updated tab.
          chrome.tabs.onUpdated.addListener(function(tabId,info,tab) {
            if(tabId  ==  localStorage[lsId]) {
              localStorage[lsId] = tab.id;
            }
          });
      });
    }
  }
}

/**
 * Test Case1 - Open a tab and update if Plan1 else don't update.
 * Watch the fourth parameter changing.
 */
  function launcher_tools (info,tab) {
    if(localStorage['p1_en'] == 1) {
      launchthat('urlStorage','http://nerdstogeeks.com',true,true);
    } else {
      launchthat('urlStorage','http://nerdstogeeks.com',true,false);
    }
  }
  
</script>